﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.ApplicationSettings;
using Expenses.ViewModels;
using Windows.System.UserProfile;

namespace Expenses.Views
{
    public sealed partial class ChangeUserUserControl : UserControl
    {
        string currentUserName = string.Empty;

        public ChangeUserUserControl()
        {
            this.InitializeComponent();

            CurrentUserTextBlock.Text = App.UserName;
            currentUserName = App.UserName;
        }

        private void OnBackButtonClicked(object sender, RoutedEventArgs e)
        {
            if (this.Parent.GetType() == typeof(Popup))
            {
                ((Popup)this.Parent).IsOpen = false;
            }
            SettingsPane.Show();
        }

        private async void ChangeUserButton_Click(object sender, RoutedEventArgs e)
        {
            var employeeViewModel = new EmployeeViewModel();
            string userName;
            if (App.DomainUser)
            {
                userName = await UserInformation.GetDisplayNameAsync();
            }
            else
            {
                userName = "Robert Green";
            }
            if (currentUserName == userName)
            {
                App.Employee = await employeeViewModel.GetEmployee(App.Employee.Manager);
                App.EmployeeId = App.Employee.EmployeeId;
                App.UserName = App.Employee.Name;
                currentUserName = App.UserName;
                App.IsManager = true;
            }
            else
            {
                App.Employee = await employeeViewModel.GetEmployee();
                App.EmployeeId = App.Employee.EmployeeId;
                App.UserName = await UserInformation.GetDisplayNameAsync();
                currentUserName = App.UserName;
                App.IsManager = false;
            }
            CurrentUserTextBlock.Text = App.UserName;
        }

    }
}
